/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Web Feeds is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

import QtQuick 1.1
import com.nokia.meego 1.0
import WebFeeds 1.0
import NewsBlur 1.0
import "../../../../shared/qml" as Shared
import "../../../../shared/qml/UIConstants.js" as UIConstants
import "../misc" as Misc

CommonNewsBlurSheet {
    id: root

    acceptButtonText: qsTr("Save")
    rejectButtonText: qsTr("Cancel")
    busyText: ""
    title: root.pageItem ? root.pageItem.title : ""
    iconSource: root.pageItem ? root.pageItem.iconSource : ""
    multilineTitle: true
    property variant __model

    onStatusChanged: {
        if((root.status == DialogStatus.Open)
           && !root.__model) {
            root.busy = true;
            service.loadFeedClassifiers(root.pageItem);
            if(root.pageItem.itemType == NewsBlurItem.StoryItem)
                storyTitleField.text = root.pageItem.title;
        }
    }

    QueryDialog {
        id: retryQueryDialog
        acceptButtonText: qsTr("Retry")
        rejectButtonText: qsTr("Cancel")
        titleText: qsTr("Network error")
        message: qsTr("Failed to retrieve the intelligence classifiers.")

        onAccepted: {
            root.busy = true;
            service.loadFeedClassifiers(root.pageItem);
        }
        onRejected: {
            root.close();
        }
    }

    Connections {
        target: root.service
        onFeedClassifiersLoaded: {
            if((result == NewsBlurEnum.SreNoError)
                    || (result == NewsBlurEnum.SreAborted)) {
                root.__model = model;
                tagRepeater.model = root.__model.tags();
                titleRepeater.model = root.__model.titles();
                authorRepeater.model = root.__model.authors();
                root.busy = false;
                contentsRootItem.visible = true;
                root.busyText = qsTr("Saving classifications")
            } else {
                root.busy = false;
                retryQueryDialog.open();
            }
        }
        onFeedClassificationsSaved: {
            if((result == NewsBlurEnum.SreNoError)
                    || (result == NewsBlurEnum.SreAborted)) {
                root.close();
                return;
            }
            root.busy = false;
            if(result == NewsBlurEnum.SreNetworkError)
                root.showMessage(qsTr("Network error"));
            else
                root.showMessage(qsTr("Failed to save classifications"));
        }
    }
    onAccepted: {
        root.busy = true;
        root.open();
        if(root.pageItem.itemType == NewsBlurItem.FeedItem)
            root.service.saveFeedClassifications(root.__model, root.pageItem.itemId);
        else if(root.pageItem.itemType == NewsBlurItem.StoryItem)
            root.service.saveFeedClassifications(root.__model, root.pageItem.feedId, root.pageItem.itemId);
    }
    onRejected: {
        if(root.busy) {
            root.open();
            root.service.abortLoadingFeedClassifiers(root.pageItem);
            root.service.abortSavingFeedClassifications();
        }
    }

    contentItem: Item {
        id: contentsRootItem
        anchors.fill: parent
        visible: false

        Flickable {
            id: flickable
            anchors.fill: parent
            anchors.bottomMargin: UIConstants.DEFAULT_MARGIN
            flickableDirection: Flickable.VerticalFlick
            contentWidth: contentColumn.width
            contentHeight: contentColumn.height
            clip: true

            Column {
                id: contentColumn
                anchors.left: parent.left
                anchors.leftMargin: UIConstants.DEFAULT_MARGIN
                width: flickable.width - (UIConstants.DEFAULT_MARGIN * 2)
                spacing: UIConstants.DEFAULT_MARGIN

                Column {
                    visible: root.pageItem ? (root.pageItem.itemType == NewsBlurItem.StoryItem) : false
                    spacing: UIConstants.DEFAULT_MARGIN
                    width: parent.width
                    Label {
                        text: qsTr("Phrases to look for in future stories")
                        wrapMode: Text.Wrap
                        width: parent.width
                        font.family: UIConstants.FONT_FAMILY_LIGHT
                    }
                    Shared.TextField {
                        id: storyTitleField
                        width: parent.width
                        onTextChanged: {
                            if(root.__model)
                                root.__model.setNewTitleClassifierText(storyTitleField.text);
                        }
                    }
                    Misc.ClassifierButton {
                        id: highlightedPhrases
                        width: parent.width
                        text: storyTitleField.text
                        classification: root.__model ? root.__model.newTitleClassifier : NewsBlurEnum.CvNeutralClassifier
                        onClassificationSet: {
                            root.__model.setNewTitleClassifierText(storyTitleField.text);
                            root.__model.newTitleClassifier = newClassification;
                            root.acceptButton.enabled = true;
                        }
                    }
                }

                Label {
                    text: qsTr("Titles and phrases")
                    visible: titleRepeater.count
                    font.family: UIConstants.FONT_FAMILY_LIGHT
                }
                Column {
                    width: parent.width
                    Repeater {
                        id: titleRepeater
                        delegate: Misc.ClassifierButton {
                            id: titleDelegate
                            width: parent ? parent.width : 0
                            property bool isFirst: !index
                            property bool isLast: (index == (titleRepeater.count - 1))
                            position: !(isFirst && isLast) ? (!isFirst ? (!isLast ? "center" : "bottom") : "top") : ""
                            text: modelData
                            classification: root.__model.titleClassification(modelData);
                            onClassificationSet: {
                                root.__model.setTitleClassification(modelData, newClassification);
                                classification = root.__model.titleClassification(modelData);
                                root.acceptButton.enabled = true;
                            }
                        }
                    }
                }

                Label {
                    text: qsTr("Authors")
                    visible: authorRepeater.count
                    font.family: UIConstants.FONT_FAMILY_LIGHT
                }
                Column {
                    width: parent.width
                    Repeater {
                        id: authorRepeater
                        delegate: Misc.ClassifierButton {
                            id: authorDelegate
                            width: parent ? parent.width : 0
                            property bool isFirst: !index
                            property bool isLast: (index == (authorRepeater.count - 1))
                            position: !(isFirst && isLast) ? (!isFirst ? (!isLast ? "center" : "bottom") : "top") : ""
                            text: modelData
                            classification: root.__model.authorClassification(modelData);
                            onClassificationSet: {
                                root.__model.setAuthorClassification(modelData, newClassification);
                                classification = root.__model.authorClassification(modelData);
                                root.acceptButton.enabled = true;
                            }
                        }
                    }
                }

                Label {
                    text: qsTr("Categories and tags")
                    visible: tagRepeater.count
                    font.family: UIConstants.FONT_FAMILY_LIGHT
                }
                Column {
                    width: parent.width
                    Repeater {
                        id: tagRepeater
                        delegate: Misc.ClassifierButton {
                            id: tagDelegate
                            width: parent ? parent.width : 0
                            property bool isFirst: !index
                            property bool isLast: (index == (tagRepeater.count - 1))
                            position: !(isFirst && isLast) ? (!isFirst ? (!isLast ? "center" : "bottom") : "top") : ""
                            text: modelData
                            classification: root.__model.tagClassification(modelData);
                            onClassificationSet: {
                                root.__model.setTagClassification(modelData, newClassification);
                                classification = root.__model.tagClassification(modelData);
                                root.acceptButton.enabled = true;
                            }
                        }
                    }
                }

                Label {
                    text: qsTr("Everything by this publisher")
                    font.family: UIConstants.FONT_FAMILY_LIGHT
                }
                Misc.ClassifierButton {
                    id: feedClassifierButton
                    width: parent.width
                    text: root.__model ? root.__model.feedTitle : ""
                    classification: root.__model ? root.__model.feedClassification : NewsBlurEnum.CvNeutralClassifier
                    onClassificationSet: {
                        root.__model.feedClassification = newClassification;
                        root.acceptButton.enabled = true;
                    }
                }
            }
        }

        ScrollDecorator {
            flickableItem: flickable
        }
    }
}
